package com.netease.financial.base.suspendwindow;

import android.content.Context;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

import com.netease.financial.R;

import cn.bingoogolapple.badgeview.BGABadgeImageView;

public class ChatBall {

    private static final String TAG = ChatBall.class.getSimpleName();
    private SuspendWindow mChatBallSuspendWindow;
    private BGABadgeImageView mChatBallView;
    private View.OnClickListener mOnChatBallClickListener;
    private Context mContext;
    private int mChatBallWidth;
    private int mChatBallHeight;

    public ChatBall(Context context) {
        mContext = context.getApplicationContext();
        init(mContext);
    }

    public void setOnChatBallClickListener(View.OnClickListener listener) {
        mOnChatBallClickListener = listener;
    }

    public void setUnreadMessageCount(int count) {
        if (count > 0) {
            mChatBallView.showTextBadge(Integer.toString(count));
        } else {
            mChatBallView.hiddenBadge();
        }
    }

    private void init(Context context) {
        mChatBallView = new BGABadgeImageView(context, null);
        mChatBallView.setImageResource(R.drawable.tooltip_icon_f);
        mChatBallWidth = mChatBallView.getDrawable().getIntrinsicWidth();
        mChatBallHeight = mChatBallView.getDrawable().getIntrinsicHeight();
        mChatBallView.setOnTouchListener(mOnTouchListener);
        mChatBallView.getDrawable().getIntrinsicHeight();
        mChatBallSuspendWindow = new SuspendWindow(context);
        mChatBallSuspendWindow.setContentView(mChatBallView);
    }

    public void onConfigurationChanged() {
        mChatBallSuspendWindow.onConfigurationChanged();
    }

    public void hideChatBall() {
        mChatBallSuspendWindow.hideContentView();
    }

    public void showChatBall() {
        mChatBallSuspendWindow.showContentView(mContext);
    }

    private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

        private boolean isMove = false;
        private Rect mViewRect = new Rect();

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isMove = false;

                    int left = mChatBallSuspendWindow.src[0] - mChatBallWidth / 2;
                    int top = mChatBallSuspendWindow.src[1] - mChatBallHeight / 2;
                    mViewRect.set(left, top, left + mChatBallWidth, top + mChatBallHeight);
                    break;

                case MotionEvent.ACTION_MOVE:
                    if (!isMove) {
                        int _lastX = (int) event.getRawX();
                        int _lastY = (int) event.getRawY() - mChatBallSuspendWindow.getStatusBarHeight(mContext);
                        if (!mViewRect.contains(_lastX, _lastY)) {
                            isMove = true;
                            mChatBallSuspendWindow.updateChatBall(event);
                        }
                    } else {
                        mChatBallSuspendWindow.updateChatBall(event);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:

                    if (isMove) {
                        mChatBallSuspendWindow.updateChatBall(event);
                    } else {
                        if (mOnChatBallClickListener != null) {
                            mOnChatBallClickListener.onClick(mChatBallView);
                        }
                    }
                    break;
            }
            return true;
        }
    };

}
